var comments = require('../models/comment'),
    ideas = require('../models/idea'),
    logger = require('../models/logger'),
    stringConstant = require('../helpers/string-constants'),
    apiHandler = require('../services/api-handler'),
    activity = require('../models/activity'),
    timeAgo = require('node-time-ago');

exports.addComment = function (req, res) {
    var userCommented = req.decoded._doc._id;
    var newComment;
    ideas.getIdeaById(req.body.ideaId, apiHandler.handler.check(res, 'NO_IDEA_FOUND', function (idea) {
        newComment = new comments({
            user: userCommented,
            idea: idea,
            comment: req.body.comment
        });
        comments.createNewComment(newComment, apiHandler.handler.check(res, 'NO_COMMENT_FOUND', function (comment) {
            ideas.updateCommentCount(idea._id);
            var newActivity = new activity({
                activityType: "Commented on",
                idea: idea,
                user: userCommented,
                comment: comment
            });
            activity.createNewActivity(newActivity, apiHandler.handler.check(res, 'ACTIVITY_FAILED', function (activity) {
                apiHandler.setSuccessResponse(comment._id, res);
            }));
        }));
    }));
};

exports.getCommentsByIdea = function (req, res) {
    comments.getCommentsByIdea(req.user._id, req.body.ideaId, req.body, apiHandler.handler.check(res, 'NO_COMMENT_FOUND', function (commentList) {
        if (comments.length == 0) {
            apiHandler.setErrorResponse('NO_COMMENT_FOUND', res);
        } else {
            commentList.forEach(function (commentData) {
                commentData.timeAgo = timeAgo(commentData.createdAt);
            });

            comments.getIdeaCommentCount(req.body.ideaId, apiHandler.handler.check(res, 'NO_COMMENT_FOUND', function (count) {
                var data = {
                    commentList: commentList,
                    commentCount: count
                };
                apiHandler.setSuccessResponse(data, res);
            }));
        }
    }));
};

exports.deleteComments = function (req, res) {
    comments.deleteCommentById(req.body.ideaId, req.body.commentId, apiHandler.handler.check(res, 'NO_COMMENT_FOUND', function (data) {
        ideas.getIdeaById(req.body.ideaId, apiHandler.handler.check(res, 'NO_IDEA_FOUND', function (idea) {
            ideas.updateCommentCount(idea._id);
            apiHandler.setSuccessResponse('', res);
        }));
    }));
};

exports.recentCommentsByUser = function (req, res) {
    comments.recentCommentsByUser(req.user._id, req.body, apiHandler.handler.check(res, 'NO_COMMENT_FOUND', function (comment) {
        if (!comment || comment.length == 0) {
            apiHandler.setErrorResponse('NO_COMMENT_FOUND', res);
        } else {
            var data = {
                commentList: comment,
                commentCount: comment.length
            };
            apiHandler.setSuccessResponse(data, res);
        }
    }));
};
